﻿using System.Xml;
using System.Web;
using System.IO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Marbles.Core.Web.Syndication
{
    public class FeedWriter
    {

        public static void Write(string Filename, IFeed Feed)
        {
            FileStream fs = new FileStream(Filename, FileMode.Create, FileAccess.Write);
            XmlDocument doc = Feed.ToXml();
            XmlWriter w = XmlWriter.Create(fs);
            doc.WriteTo(w);
            w.Flush();
            w.Close();
            fs.Close();
        }

        public static void Write(HttpResponse Response, IFeed Feed, string Filename)
        {        
            XmlDocument doc = Feed.ToXml();
            Response.AddHeader("Content-Disposition",String.Format("attachment;filename={0}", Filename));
            XmlWriter w = XmlWriter.Create(Response.OutputStream);
            Response.ContentType = Feed.ContentType;
            doc.WriteTo(w);
            w.Flush();
            w.Close();
        }

        public static void Write(HttpResponse Response, IFeed Feed)
        {
            XmlDocument doc = Feed.ToXml();
            XmlWriter w = XmlWriter.Create(Response.OutputStream);
            Response.ContentType = Feed.ContentType;
            doc.WriteTo(w);
            w.Flush();
            w.Close();
        }

        public static void Write(Stream Stream, IFeed Feed)
        {
            XmlDocument doc = Feed.ToXml();
            XmlWriter w = XmlWriter.Create(Stream);
            doc.WriteTo(w);
            w.Flush();
            w.Close();
        }
    }
}
